﻿
// 2019/4/22: 首个记录
// 2019/9/19: 增加USoundSensorProperty默认构造函数
// 2019/10/11: 修改pragma once和include
// 2020/1/14: 接口完整性
// 2020/11/12: 支持Spadas7

#ifndef USOUND_SAMPLE_V1_H
#define USOUND_SAMPLE_V1_H

#include "spadas.h"

namespace usound_sample_v1
{
	using namespace spadas;

#if SPADAS_VERSION_MAJOR < 7
	typedef OptionalDouble GeneralElement;
#endif

	struct USoundSensorProperty
	{
		Double originX; // 传感器坐标原点
		Double originY; // 传感器坐标原点
		Double orientation; // 传感器中心轴朝向
		Double angleRangeAlpha; // α角范围 [°]（一般在100°左右，以OriginX/Y为圆心）
		Double angleRangeBeta; // β角范围 [°]（一般在0~30°）
		Double distanceRangeAlpha; // α角对应的探测距离 [m]（较短）
		Double distanceRangeBeta; // β角对应的探测距离 [m]（较长）

		USoundSensorProperty()
		{
			originX = 0;
			originY = 0;
			orientation = 0;
			angleRangeAlpha = 100;
			angleRangeBeta = 15;
			distanceRangeAlpha = 0.5;
			distanceRangeBeta = 2.5;
		}
	};

	struct USoundSensorData
	{
		Int id; // 传感器ID，从1开始
		USoundSensorProperty properties; // 超声波传感器属性
		OptionalDouble distance; // 障碍物的距离，单位m，无障碍物则为空
	};

	struct USoundSample
	{
		GlobalTimestamp timeStamp; // 时间戳
		Array<USoundSensorData> dataList; // 各个超声波传感器的数据

		USoundSample()
		{}

		static String getProtocolName()
		{
			return "usound-sample-v1";
		}

		static String getTitle()
		{
			return "Ultrasound sensor count,First sensor's ID,First sensor's detected distance[m],First sensor's origin-X[m],First sensor's origin-Y[m],First sensor's orientation[°],First sensor's α angle range[°],First sensor's β angle range[°],First sensor's α distance range[m],First sensor's β distance range[m],Second sensor's ID,etc";
		}

		static Bool supportInterpolation()
		{
			return FALSE;
		}

		GeneralSample toGeneralSample()
		{
			GeneralSample sample;
			sample.timeStamp = timeStamp;

			sample.values = Array<GeneralElement>(1 + 9 * dataList.size());
			sample.values[0] = dataList.size();

			for (UInt i = 0; i < dataList.size(); i++)
			{
				USoundSensorData& src = dataList[i];
				sample.values[1 + 9 * i] = src.id;
				sample.values[2 + 9 * i] = src.distance;
				sample.values[3 + 9 * i] = src.properties.originX;
				sample.values[4 + 9 * i] = src.properties.originY;
				sample.values[5 + 9 * i] = src.properties.orientation;
				sample.values[6 + 9 * i] = src.properties.angleRangeAlpha;
				sample.values[7 + 9 * i] = src.properties.angleRangeBeta;
				sample.values[8 + 9 * i] = src.properties.distanceRangeAlpha;
				sample.values[9 + 9 * i] = src.properties.distanceRangeBeta;
			}

			sample.significantCount = sample.values.size();
			return sample;
		}

		Bool fromGeneralSample(GeneralSample sample)
		{
			// 未实现
			return FALSE;
		}

		Bool fromGeneralSample(String protocol, GeneralSample sample)
		{
			// 未实现
			return FALSE;
		}

		static USoundSample interpolate(USoundSample& s1, Double w1, USoundSample& s2, Double w2, GlobalTimestamp time)
		{
			return USoundSample();
		}
	};
}

#endif